package com.naiterui.ehp.ps.push.utils.umeng;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.ps.push.utils.umeng.android.AndroidBroadcast;
import com.naiterui.ehp.ps.push.utils.umeng.android.AndroidGroupcast;
import com.naiterui.ehp.ps.push.utils.umeng.android.AndroidNotification;
import com.naiterui.ehp.ps.push.utils.umeng.android.AndroidUnicast;

@Component
public class PharmacistAndroidPusher {
    private String androidappkey = ConfigUtil.getString("pharmacist.android.push.umeng.appkey");
    private String androidappMasterSecret = ConfigUtil.getString("pharmacist.android.push.umeng.mastersecret");
    private String androidpackage = ConfigUtil.getString("pharmacist.android.push.umeng.package") + ".activity.LoadActivity";
    private String commonTitle = ConfigUtil.getString("pharmacist.push.umeng.commontitle");
    private static PushClient client = new PushClient();

    /**
     * android 全量推送
     *
     * @throws Exception
     */
    public void sendAndroidBroadcast(String text, String linkUrl) throws Exception {
        AndroidBroadcast broadcast = new AndroidBroadcast(androidappkey, androidappMasterSecret);
        broadcast.setTicker(commonTitle);
        broadcast.setTitle(commonTitle);
        broadcast.setText(text);
        //broadcast.goActivityAfterOpen(androidpackage);
        broadcast.goAppAfterOpen();
        broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        broadcast.setProductionMode();
        broadcast.setmipush(androidpackage);
        broadcast.setExtraField("custom", linkUrl);
        client.send(broadcast);
    }

    /**
     * android 单个推送
     *
     * @throws Exception
     */
    public void sendAndroidUnicast(String deviceToken, String text, String linkUrl) throws Exception {
        AndroidUnicast unicast = new AndroidUnicast(androidappkey, androidappMasterSecret);
        unicast.setDeviceToken(deviceToken);
        unicast.setTicker(commonTitle);
        unicast.setTitle(commonTitle);
        unicast.setText(text);
        //unicast.goActivityAfterOpen(androidpackage)
        unicast.goAppAfterOpen();
        unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        unicast.setProductionMode();
        unicast.setmipush(androidpackage);
        unicast.setCustomField(linkUrl);
        unicast.setExtraField("custom", linkUrl);
        client.send(unicast);
    }

    /**
     * android 标签-批量推送 TODO
     *
     * @throws Exception
     */
    public void sendAndroidGroupcast(String text, String linkUrl, List<String> tags) throws Exception {
        AndroidGroupcast groupcast = new AndroidGroupcast(androidappkey, androidappMasterSecret);
        /*
         *  Construct the filter condition:
         *  "where":
         *	{
         *		"and":
         *		[
         *			{"tag":"test"},
         *			{"tag":"Test"}
         *		]
         *	}
         */
        JSONObject filterJson = new JSONObject();
        JSONObject whereJson = new JSONObject();
        JSONArray tagArray = new JSONArray();
        tags.forEach(tag -> {
            JSONObject tagJson = new JSONObject();
            try {
                tagJson.put("tag", tag);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            tagArray.put(tagJson);
        });
        whereJson.put("and", tagArray);
        filterJson.put("where", whereJson);

        groupcast.setFilter(filterJson);
        groupcast.setTicker(commonTitle);
        groupcast.setTitle(commonTitle);
        groupcast.setText(text);
        groupcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        groupcast.setProductionMode();
        groupcast.goAppAfterOpen();
        //groupcast.goActivityAfterOpen(androidpackage);
        groupcast.setmipush(androidpackage);
        groupcast.setExtraField("custom", linkUrl);
        client.send(groupcast);
    }

    /*
    public void sendAndroidCustomizedcast() throws Exception {
        AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(androidappkey, androidappMasterSecret);
        customizedcast.setAlias("alias", "alias_type");
        customizedcast.setTicker("Android customizedcast ticker");
        customizedcast.setTitle("中文的title");
        customizedcast.setText("Android customizedcast text");
        customizedcast.goAppAfterOpen();
        customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        customizedcast.setProductionMode();
        client.send(customizedcast);
    }*/

    /*
    public void sendAndroidCustomizedcastFile() throws Exception {
        AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(androidappkey, androidappMasterSecret);
        // And if you have many alias, you can also upload a file containing these alias, then
        // use file_id to send customized notification.
        String fileId = client.uploadContents(androidappkey, androidappMasterSecret, "aa" + "\n" + "bb" + "\n" + "alias");
        customizedcast.setFileId(fileId, "alias_type");
        customizedcast.setTicker("Android customizedcast ticker");
        customizedcast.setTitle("中文的title");
        customizedcast.setText("Android customizedcast text");
        customizedcast.goAppAfterOpen();
        customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        // For how to register a test device, please see the developer doc.
        customizedcast.setProductionMode();
        client.send(customizedcast);
    }

    public void sendAndroidFilecast() throws Exception {
        AndroidFilecast filecast = new AndroidFilecast(androidappkey, androidappMasterSecret);
        String fileId = client.uploadContents(androidappkey, androidappMasterSecret, "aa" + "\n" + "bb");
        filecast.setFileId(fileId);
        filecast.setTicker("Android filecast ticker");
        filecast.setTitle("中文的title");
        filecast.setText("Android filecast text");
        filecast.goAppAfterOpen();
        filecast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        client.send(filecast);
    }
    */
}
